package com.lxq.LeetCode.removeNthFromEnd;

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public static void main(String[] args) {

    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pLoca = head, pOper = null;
        if (head.next == null && n == 1) {
            return null;
        }
        while (n > 0) {
            pLoca = pLoca.next;
            n--;
        }
        pOper = head;
        if (pLoca == null) {
            head = head.next;
        }else {
            while (pLoca.next != null) {
                pLoca = pLoca.next;
                pOper = pOper.next;
            }
            pOper.next = pOper.next.next;
        }

        return head;
    }
}
